package com.itheima.exam.service.impl;


import com.itheima.exam.mapper.ClassMapper;
import com.itheima.exam.mapper.StudentMapper;
import com.itheima.exam.pojo.entity.Student;
import com.itheima.exam.pojo.vo.StudentVo;
import com.itheima.exam.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    ClassMapper classMapper;

    @Override
    public List<StudentVo> page(int currentPage, int pageSize, Student student) {
        //计算当前页开始索引 LIMIT
        int startIndex = (currentPage - 1) * pageSize;
        //所有的学生数据
        List<Student> studentList = studentMapper.page(startIndex, pageSize, student);
        List<StudentVo> voList = studentList.stream()
                .map(stu -> {
                    StudentVo vo = new StudentVo();
                    BeanUtils.copyProperties(stu,vo);
                    String cname = classMapper.findCnameByCid(stu.getCid());
                    vo.setCname(cname);
                    return vo;
                })
                .collect(Collectors.toList());
        return voList;
    }

    @Override
    public int rows(Student student) {
        return studentMapper.rows(student);
    }


//    @Override
//    public List<StudentVo> page(int currentPage, int pageSize, Student student) {
//        //计算当前页开始索引 LIMIT
//        int startIndex = (currentPage - 1) * pageSize;
//        //所有的学生数据
//        List<Student> studentList = studentMapper.page(startIndex, pageSize, student);
//        List<StudentVo> voList = new ArrayList<>();
//        for (Student stu : studentList) {
//            StudentVo studentVo = new StudentVo();
//            //根据学生cid查询班级名称
//            String cname = classMapper.findCnameByCid(stu.getCid());
//            //将stu数据拷贝到vo中
//            BeanUtils.copyProperties(stu,studentVo);
//            //将班级名称设置给vo
//            studentVo.setCname(cname);
//            //把所有vo收集起来
//            voList.add(studentVo);
//        }
//
//        return voList;
//    }
}
